package com.LeeCode;

/**
 * 最长回文子序列
 */

public class Code516 {
    public static void main(String[] args) {
        String s = "cbbd";
        System.out.println(new Code516().longestPalindromeSubseq(s));
    }

    public int longestPalindromeSubseq(String s) {
        int n = s.length();
        int[][] dp = new int[n][n];
        for (int i = 0; i < n; i++) {
            dp[i][i] = 1;
        }
        for (int i = n - 2; i >= 0; i--) {
            for (int j = i + 1; j < n; j++) {
                int aux = s.charAt(i) == s.charAt(j) ? 2 : 0;
                dp[i][j] = Math.max(dp[i + 1][j - 1] + aux, Math.max(dp[i + 1][j], dp[i][j - 1]));
            }
        }
        return dp[0][n - 1];
    }
}
